/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressBook.baseService;

import com.addressBook.bean.UserInfo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Add {

    /**
     * 用于新增一个用户时,追加到文件中,不过不知道用不用好??
     *
     * @param file
     * @param str
     */
    public static void appendFile(File file, String str) {
        try {
            FileOutputStream fos = new FileOutputStream(file, true);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
            osw.write(str);
            osw.flush();
            osw.close();
            fos.close();
        } catch (FileNotFoundException ex) {
            System.out.println("在追加文件时出现打开错误.");
            Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            System.out.println("oupstreamwriter编码出错.");
            Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            System.out.println("osw写入时出错.");
            Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 用于追加一个bean到现有的bean集合中.不过都不知道用不用好
     *
     * @param beans
     * @param bean
     * @return
     */
    public static ArrayList appendBean(ArrayList<UserInfo> beans, UserInfo bean) {
        beans.add(bean);
        return beans;
    }

    public static ArrayList newFile() {
        ArrayList<UserInfo> beans=new ArrayList<UserInfo>();
        return beans;
    }
}
